package com.yzh.springboot_rbac.controller;

import com.yzh.springboot_rbac.entity.Role;
import com.yzh.springboot_rbac.service.RoleService;
import com.yzh.springboot_rbac.utils.AjaxResult;
import com.yzh.springboot_rbac.utils.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色管理功能模块
 */
@RestController
@RequestMapping("/role")
public class RoleController extends BaseController {
    @Autowired
    private RoleService roleService;
    /**
     * 1.新增角色
     * 条件：角色更新完，角色菜单关系表更新
     */
    @PostMapping("/addRole")
    public AjaxResult addRole(@RequestBody Role role, @RequestParam("menuId") Integer menuId) throws Exception {
        Boolean flag = roleService.isAddRole(role, menuId);
        if (flag) {
            return success(role);
        } else {
            return error();
        }
    }

    /**
     * 2.查询角色
     * 条件根据条件查询，条件是shiro上面的条件
     */
    @PostMapping("/selectRoleByCondition")
    public AjaxResult selectRoleByCondition(@RequestBody Role role) {
        List<Role> roles = roleService.selectRoleByCondition(role);
        if (roles != null) {
            return success(roles);
        } else {
            return error();
        }
    }

    /**
     * 3.更新角色
     * 条件：也要更新角色-菜单关系表
     */
    @PostMapping("/updateRole")
    public AjaxResult updateRole(@RequestBody Role role, @RequestParam("menuId") Integer menuId) throws Exception {
        Boolean flag = roleService.isUpdateRole(role, menuId);
        if (flag) {
            return success();
        } else {
            return error();
        }
    }

    /**
     * 4.删除角色
     * 条件：同时也要删除角色-菜单关系表
     */
    @PostMapping("/deleteRole")
    public AjaxResult deleteRole(@RequestBody Role role) throws Exception {
        Boolean flag = roleService.deleteRole(role);
        if (flag) {
            return success();
        } else {
            return error();
        }
    }
}
